﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ces.Entity;
using MESQWinform;
using Newtonsoft.Json;

namespace MesProjectWinform.BasicData.基础实现.显示页面
{
    public partial class chejianadd : DevExpress.XtraEditors.XtraForm
    {
        public chejianadd()
        {
            InitializeComponent();
            
            // 设置窗体居中显示
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            // 清空输入框内容
            textEdit1.Text = "";
            textEdit2.Text = "";
            memoEdit1.Text = "";
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 验证必填字段
                if (string.IsNullOrWhiteSpace(textEdit1.Text))
                {
                    XtraMessageBox.Show("请输入车间编码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrWhiteSpace(textEdit2.Text))
                {
                    XtraMessageBox.Show("请输入车间名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 创建请求对象
                var requestData = new
                {
                    workshopcode = textEdit1.Text.Trim(),
                    workshopname = textEdit2.Text.Trim(),
                    workshopdescription = memoEdit1.Text.Trim()
                };

                // 序列化为JSON
                string jsonData = JsonConvert.SerializeObject(requestData);

                // 调用API - 确保URL大小写正确
                string result = await APIClientl.CallApiAsync("api/WorkShop/AddWorkShop", "POST", jsonData);

                // 解析返回结果
                var response = JsonConvert.DeserializeObject<WorkShopDto>(result);

                if (response != null && response.Code == "200")
                {
                    // 显示成功提示
                    XtraMessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                    // 设置对话框结果为OK，以便调用者知道操作成功
                    this.DialogResult = DialogResult.OK;
                    
                    // 关闭当前窗口
                    this.Close();
                }
                else
                {
                    string message = response != null ? response.Message : "未知错误";
                    XtraMessageBox.Show($"添加失败: {message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"添加失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 编码后的文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            // 编码文本框内容变化时的处理
        }
    }
}